package com.gsk.IOCcontain;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import com.gsk.Exception.NoSuchPropertyException;

public class BeanParser {

    static MiniApplicationContext mac;

    public static MiniApplicationContext getMiniApplicationContext(List<BeanDefinition> beanDefinitionList) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchPropertyException, NoSuchFieldException {

        mac=new MiniApplicationContext();

        for(BeanDefinition beanDefinition:beanDefinitionList){
            createBean(beanDefinition,beanDefinitionList);
        }

        return mac;
    }

    public static void createBean(BeanDefinition beanDefinition,List<BeanDefinition> beanDefinitionList) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException, NoSuchPropertyException {
        if(mac.beans.containsKey(beanDefinition.getId())) {
            //如果这个bean已经在其他递归中被加载过了
            return;
        }else {
            Class newClass = Class.forName(beanDefinition.getClassName());
            Object object=newClass.newInstance();
            if (beanDefinition.getPropertites() == null) {
                mac.beans.put(beanDefinition.getId(), object);
            } else {
                for(String key:beanDefinition.getPropertites().keySet()){
                    for(String key1:beanDefinition.getPropertites().get(key).keySet()){
                        //依赖注入
                        if(key1.equals("ref")){
                            if(mac.beans.containsKey(key)){
                                Method[] declaredMethods = newClass.getDeclaredMethods();
                                for(Method method:declaredMethods){
                                    if(method.getName().equals("set"+key.substring(0,1).toUpperCase()+key.substring(1))){
                                        method.invoke(object,mac.beans.get(key));
                                    }
                                }
                            }else {
                                for(BeanDefinition beanDefinition1:beanDefinitionList){
                                    if(beanDefinition.getId().equals(key)){
                                        createBean(beanDefinition1,beanDefinitionList);
                                    }
                                }
                            }
                        }else {
                            Method[] declaredMethods = newClass.getDeclaredMethods();
                            try{
                                Field field = newClass.getDeclaredField(key);
                            }catch (Exception e){
                                throw new NoSuchPropertyException(newClass.getName()+"不存在"+key+"这个属性");
                            }
                            for(Method method:declaredMethods){
                                if(method.getName().equals("set"+key.substring(0,1).toUpperCase()+key.substring(1))){
                                    method.invoke(object,beanDefinition.getPropertites().get(key).get(beanDefinition.getPropertites().get(key).keySet().toArray()[0]));
                                }
                            }
                        }
                    }
                }

                mac.beans.put(beanDefinition.getId(), object);
            }
        }
    }
}

